package com.afd.ozblip.activity;

import com.afd.ozblip.entity.NoticeSetEntity;
import com.afd.ozblip.util.SharedUtil;

import android.R.bool;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;

public class MessageNoticeActivity extends BaseActivity {

	/****************** 视图变量 **************************/
	private ToggleButton activityTB;
	private ToggleButton noticeTB;
	private ToggleButton updateTB;
	private ToggleButton msgTB;
	private LinearLayout timeLayout;
	private TimePicker beginTimeTp;
	private TimePicker endTimeTp;
	private TextView beginTimeTv;
	private TextView endTimeTv;

	/****************** 普通变量 **************************/
	private String beginTimeStr;
	private String endTimeStr;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notice_setting);
		// 启动返回功能
		toBack();
		// 寻找视图
		findViews();
		// 设置监听
		setListeners();
		// 获取参数
		getBundle();
	}

	/**
	 * 寻找视图
	 */
	public void findViews() {
		this.activityTB = (ToggleButton) findViewById(R.id.tb_setting_activity);
		this.noticeTB = (ToggleButton) findViewById(R.id.tb_setting_notice);
		this.updateTB = (ToggleButton) findViewById(R.id.tb_setting_update);
		this.msgTB = (ToggleButton) findViewById(R.id.tb_setting_message);
		this.timeLayout = (LinearLayout) findViewById(R.id.ll_notice_time);
		this.beginTimeTv = (TextView) findViewById(R.id.tv_notice_beginTime);
		this.endTimeTv = (TextView) findViewById(R.id.tv_notice_endTime);
		// 获取shared存储的值
		NoticeSetEntity nse = SharedUtil.fetchNoticeInfo(this);
		if (nse != null) {
			this.activityTB.setChecked(nse.isActivityFlag());
			this.noticeTB.setChecked(nse.isNoticeFlag());
			this.updateTB.setChecked(nse.isUpdateFlag());
			this.msgTB.setChecked(nse.isMessageFlag());
			this.beginTimeTv.setText(nse.getBeginTime());
			this.endTimeTv.setText(nse.getEndTime());
		}
		this.beginTimeStr = this.beginTimeTv.getText().toString();
		this.endTimeStr = this.endTimeTv.getText().toString();
	}

	/**
	 * 设置监听
	 */
	public void setListeners() {
		View.OnClickListener moreClick = new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.ll_notice_time:
					final Dialog dlg = new Dialog(MessageNoticeActivity.this,
							R.style.listDialog);
					dlg.show();
					Window window = dlg.getWindow();
					// *** 主要就是在这里实现这种效果的.
					// 设置窗口的内容页面,notice_time.xml文件中定义view内容
					window.setContentView(R.layout.notice_time);
					// 初始视图
					beginTimeTp = (TimePicker) window
							.findViewById(R.id.tp_notice_beginTime);
					endTimeTp = (TimePicker) window
							.findViewById(R.id.tp_notice_endTime);
					String[] btArr = beginTimeStr.split(":");
					beginTimeTp.setCurrentHour(Integer.parseInt(btArr[0]));
					beginTimeTp.setCurrentMinute(Integer.parseInt(btArr[1]));
					String[] etArr = endTimeStr.split(":");
					endTimeTp.setCurrentHour(Integer.parseInt(etArr[0]));
					endTimeTp.setCurrentMinute(Integer.parseInt(etArr[1]));
					// 为确认按钮添加事件,执行退出应用操作
					Button ok = (Button) window
							.findViewById(R.id.btn_notice_ok);
					ok.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							String bTmin = beginTimeTp.getCurrentMinute() < 10 ? "0"
									+ beginTimeTp.getCurrentMinute()
									: beginTimeTp.getCurrentMinute() + "";
							beginTimeTv.setText(beginTimeTp.getCurrentHour()
									+ ":" + bTmin);
							String eTmin = endTimeTp.getCurrentMinute() < 10 ? "0"
									+ endTimeTp.getCurrentMinute()
									: endTimeTp.getCurrentMinute() + "";
							endTimeTv.setText(endTimeTp.getCurrentHour() + ":"
									+ eTmin);
							beginTimeStr = beginTimeTv.getText().toString();
							endTimeStr = endTimeTv.getText().toString();
							// 存储通知设置
							storeNoticeInfo();
							dlg.cancel();

						}
					});
					// 关闭alert对话框架
					Button cancel = (Button) window
							.findViewById(R.id.btn_notice_cancel);
					cancel.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							dlg.cancel();
						}
					});
					
					break;

				default:
					break;
				}
			}
		};
		this.timeLayout.setOnClickListener(moreClick);
		CompoundButton.OnCheckedChangeListener noticeTb = new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// 存储通知设置
				storeNoticeInfo();
			}
		};
		this.activityTB.setOnCheckedChangeListener(noticeTb);
		this.noticeTB.setOnCheckedChangeListener(noticeTb);
		this.updateTB.setOnCheckedChangeListener(noticeTb);
		this.msgTB.setOnCheckedChangeListener(noticeTb);
	}

	/**
	 * 获取传递的参数
	 */
	private void getBundle() {
		// Intent intent = getIntent();
	}

	private void storeNoticeInfo() {
		NoticeSetEntity nse = new NoticeSetEntity();
		nse.setActivityFlag(activityTB.isChecked());
		nse.setNoticeFlag(noticeTB.isChecked());
		nse.setUpdateFlag(updateTB.isChecked());
		nse.setMessageFlag(msgTB.isChecked());
		nse.setBeginTime(beginTimeStr);
		nse.setEndTime(endTimeStr);
		SharedUtil.storeNoticeInfo(this, nse);
	}
}